<?php
$key = ftok("42.php","x");
$msqid = msg_get_queue($key);
print_r(msg_stat_queue($msqid));

$pid = pcntl_fork();
if ($pid==0){
    $i=1;
    while (1){
       $ret =  msg_receive($msqid,0,$msgType,1024,$msg,true,MSG_IPC_NOWAIT,$error);

        if ($error!=MSG_ENOMSG){
            echo $msg."\r\n";
        }
    }
    exit(0);
}

$i = 1;
while (1){
    echo msg_send($msqid,2,"hello",true,true);
    sleep(1);
    if ($i++==3){
        posix_kill($pid,SIGKILL);
        break;
    }
}

$pid = pcntl_wait($status);
if ($pid>0){
    fprintf(STDOUT,"ext pid=%d\n",$pid);
}

if (msg_remove_queue($msqid)){
    echo "remove ok\n";
}
